package com.fs.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.fs.common.constant.CacheConstants;
import com.fs.common.core.pojo.SysDictData;
import com.fs.common.util.RedisCache;
import com.fs.system.mapper.SysDictDataMapper;
import com.fs.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        //1.先从缓存获取,如果缓存直接返回
        String chacheKey = getChacheKey(dictType);
        List<SysDictData> sysDictDataList = redisCache.getCacheObject(chacheKey);
        if(CollectionUtil.isNotEmpty(sysDictDataList)){
            return sysDictDataList;
        }
        //2.如果缓存没有, 到数据库查询
        sysDictDataList = sysDictDataMapper.selectDictDataByType(dictType);
        if(CollectionUtil.isNotEmpty(sysDictDataList)){
            redisCache.setCacheObject(chacheKey,sysDictDataList);
            return sysDictDataList;
        }

        return null;
    }

    //构建缓存key
    private  String  getChacheKey(String dictType){
        return CacheConstants.SYS_DICT_KEY+dictType;
    }

}
